package at.horario.web.pages;

import at.horario.model.Horario;
import at.horario.model.Professor;
import at.horario.service.CelulaManager;
import at.horario.service.HorarioManager;
import at.horario.service.ProfessorManager;
import at.horario.service.TurmaManager;
import at.horario.web.property.editor.XlsLink;
import static org.apache.commons.lang.StringUtils.EMPTY;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.IDataProvider;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.apache.wicket.authorization.strategies.role.annotations.AuthorizeInstantiation;


@AuthorizeInstantiation({"user", "admin"})

public class QuadroForm extends PageTemplate {
    @SpringBean(name = "horarioManager")
    private HorarioManager horarioManager;
    @SpringBean(name = "celulaManager")
    private CelulaManager celulaManager;
    @SpringBean(name = "turmaManager")
    private TurmaManager turmaManager;
    @SpringBean(name = "professorManager")
    private ProfessorManager professorManager;
    private Horario horario;


    /**
     * Provides a composite User Actions panel for the Actions column.
     *
     * @author igor
     */
    private static class UserActionsPanel extends Panel {
        public UserActionsPanel(String id, IModel model) {
            super(id);
            addEditLink(model);
            addDeleteLink(model);
            addProcessLink(model);
        }

        private void addDeleteLink(IModel model) {
            add(new Link("deleteLink", model) {
                /**
                 * Go to the Delete page, passing this page and the id of the
                 * Contact involved.
                 */
                @Override
                public void onClick() {
                    //setResponsePage(new DeleteHorarioPage(getPage(), getModel()));
                }
            });
        }

        private void addEditLink(IModel model) {
            add(new Link("editLink", model) {
                /**
                 * Go to the Edit page, passing this page and the id of the
                 * Contact involved.
                 */
                @Override
                public void onClick() {
                    setResponsePage(new HorarioForm(getPage(), getModel()));
                }
            });
        }

        private void addProcessLink(IModel model) {
            add(new Link("processLink", model) {
                /**
                 * Go to the Edit page, passing this page and the id of the
                 * Contact involved.
                 */
                @Override
                public void onClick() {
                    setResponsePage(new HorarioScheduleForm(getPage(), (Horario) getModel().getObject()));
                }
            });
        }

    }


    public QuadroForm(Horario horario) {
        // Form and button for routing horario to add a new horario
        Form form = new Form("form");
        Button button = new Button("add-horario") {
            public void onSubmit() {
                onEditHorario(new Model(new Horario()));
            }
        };
        button.setDefaultFormProcessing(false);
        form.add(button);
        add(form);
        IDataProvider dataProvider = new ProfessorRowDataProvider(horario, celulaManager);
        add(new XlsLink("horarioxls", dataProvider));
        final DataView dataView = new QuadroDataView("quadro", dataProvider);
        add(dataView);
    }


    /**
     * Listener for the edit action
     *
     * @param horario horario to be edited
     */
    protected void onEditHorario(IModel model) {
        setResponsePage(new HorarioForm(this, model));
    }

    protected void onProcessar(Horario horario) {
        //TODO: criar HorarioScheduleForm
        //setResponsePage(new HorarioScheduleForm(this, horario));
    }

    private class QuadroDataView extends DataView {
        public QuadroDataView(String id, IDataProvider dataProvider) {
            super(id, dataProvider);
        }

        protected void populateItem(Item item) {
            ProfessorRow row = (ProfessorRow) item.getModelObject();
            Professor professor = professorManager.findBy(row.getHorario(), row.getProfessor());
            item.add(new Label("cel", new Model(professor.toString())));
                for (int i = 0; i < row.getHorarios().length; i++) {
                    int i1 = row.getHorarios()[i];
                    String value;
                    if (i1 > 0) {
                        value = turmaManager.findBy(row.getHorario(), i1).toString();
                    } else if (i1 == 0) {
                        value = EMPTY;
                    } else
                        value = "X";
                    String.valueOf(i1);
                    item.add(new Label("cel" + (i + 1), new Model(value)));
                }
        }
    }
}